$(document).ready(function() {
	
	function mostrarOp(idTR, s) {
		if(s)
			$("#"+idTR+" .edit, #"+idTR+" .delete").css("display", "block");
		else
			$("#"+idTR+" .edit, #"+idTR+" .delete").css("display", "none");
	}
	
	function carregarFuncoes() {
		
		$("#busca").quicksearch("#homeLogadoContainer .conteudo-top table tbody tr");
		
		$("#print").click(print);
		
		$(".tabelaDefault tbody tr").hover(function() {
			mostrarOp(this.id, true); //in
		}, function() {
			mostrarOp(this.id, false); //out
		});
		
		$(".tabelaDefault tbody td").click(function() {
			
			var linha = $(this).parent().attr("id");
			var id = $(this).parent().attr("value");
			var edit = $(this).hasClass("edit");
			var del = $(this).hasClass("delete");
			
			if(edit)
				editar(id);
			else if(del){
				if(confirm("Remover paciente?"))
					deletar(id, linha);
			} else
				exibir(id);

		});
	}
	
	function montarTabela(j) {
		var linha;
		//$("#homeLogadoContainer .conteudo-top").html("<input type='text' id='busca' /><input type='button' value='Print' id='print' /><br /><table align='left' class='tabelaDefault'><thead><tr><th class='tam5'>Nome</th></tr></thead><tbody></tbody></table>");
		$("#homeLogadoContainer .conteudo-top").html("<table align='left' class='tabelaDefault'><thead><tr><th class='tam5'>Nome</th></tr></thead><tbody></tbody></table>");
		
		for(i = 0; i < j.length; i++) {
	    	linha = (i%2==0?
	    			"<tr id='"+i+"' value='" + j[i].id + "' class='linhaA'><td>"+j[i].nome+"</td><td title='editar' class='edit'></td><td title='remover' class='delete'></td></tr>":
	    			"<tr id='"+i+"' value='" + j[i].id + "' class='linhaB'><td>"+j[i].nome+"</td><td title='editar' class='edit'></td><td title='remover' class='delete'></td></tr>");
	    	$("#homeLogadoContainer .conteudo-top table tbody").append(linha);
	    }
	}
	
	function listar() {
		$.ajax({
			url: "/Fisio-web/paciente",
			data: {
				op: "listar"
			},
	    	success: function(jsonArray) {
				montarTabela(jsonArray);
				carregarFuncoes();
	    	}
		});
	}
	
	function print(){
		var busca = $("#busca").val();
		var selected = $("#homeLogadoContainer .conteudo-top table tbody").children();
		var txt = busca+"\n";
		selected.each(function(i, ele) {
			if($(ele).css("display") !== "none")
				txt += $(ele).children('td:first').text()+"\n";
		});
		alert(txt);
	}
	
	function deletar(id, linha) { //via POST
		$.ajax({
			type: "POST",
			url: "/Fisio-web/paciente",
			data: {
				op: "deletar",
				id: id
			},
	    	success: function() {
				$(".tabelaDefault #"+linha).remove();
	    	}
		});
	}
	
	function editar(id) {
		location.href = "/Fisio-web/paciente?op=editar&id="+id; //via GET
	}
	
	function exibir(id) {
		location.href = "/Fisio-web/paciente?op=exibir&id="+id; //via GET
	}
	
	$("#tabPacientes").click(function() {
		listar();
	});
	
	/* DINAMISMO */
	if($.tabAtiva == "#tabPacientes")
		listar();
});